<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;


/**
 * 用户微信信息临时表
 * Class ArticleTypeModel
 * @package app\common\model
 */
class UserWechatInfoTemp extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';

    protected $table = 'user_wechat_info_temp';



    /**
     * 登录或修改微信信息 
     * @param open_id open_id
     */
    public function change($data, $field = [], $findWhere = [])
    {
        $res = $this->where('open_id', $data['open_id'])->first();
        if (empty($res)) {
            $res = $this;

            //已经存在的不允许修改昵称
            $res->open_id = $data['open_id'];
            $res->union_id = $data['union_id'];
            $res->source_invite = $data['invite'];
        }
        $res->nickname = $data['nickname'];

        if (!empty($data['head_img'])) {
            $res->head_img = $data['head_img'];
        } else {
            //没有头像获取一个默认头像
            $userInfoModel = new UserInfo();
            $res->head_img = $userInfoModel->getDefaultHeadImg();
        }


        $res->save();
        return true;
    }
}
